package message

import "errors"

// ResultMessage 返回响应
type ResultMessage[T any] struct {
	Code    HttpCode `json:"code"`
	Message string   `json:"message"`
	Data    T        `json:"data"`
}

// PageRequest 分页请求
type PageRequest struct {
	Page     int `json:"page"`
	PageSize int `json:"pageSize"`
}

// PageResponse 分页响应
type PageResponse[T any] struct {
	Page     int   `json:"page"`
	PageSize int   `json:"pageSize"`
	Total    int64 `json:"total"`
	List     []T   `json:"list"`
}

type HttpCode int64

const (
	Success              HttpCode = 100200
	Failed               HttpCode = 100500
	LoginFailed          HttpCode = 100499
	DataNotExist         HttpCode = 100501
	UserPasswordError    HttpCode = 100502
	AuthNotExist         HttpCode = 100503
	AuthFail             HttpCode = 100504
	RequestParamError    HttpCode = 100505
	UserNameNotExist     HttpCode = 100506
	UserNameExist        HttpCode = 100507
	PhoneExist           HttpCode = 100508
	EmailExist           HttpCode = 100509
	TokenBuildError      HttpCode = 100510
	TokenTimeOut         HttpCode = 100511
	AddDataError         HttpCode = 100512
	SqlExecuteError      HttpCode = 100513
	DeleteSuccess        HttpCode = 100514
	ExistSameData        HttpCode = 100515
	SoftwareCreateError  HttpCode = 100516
	DataNotNeedUpdate    HttpCode = 100517
	DataDeleteFail       HttpCode = 100518
	LogoutSuccess        HttpCode = 100519
	LogoutFail           HttpCode = 100520
	DateUpdateError      HttpCode = 100521
	CaptchaImageError    HttpCode = 100522
	UserNotAllowDelete   HttpCode = 100523
	LoginSuccess         HttpCode = 100524
	RegisterReqParseFail HttpCode = 100525
	RegisterUserFail     HttpCode = 100526
	QueryDataFail        HttpCode = 100527
	UserForbidden        HttpCode = 100528
	SystemFail           HttpCode = 100529
	RefreshTokenFail     HttpCode = 100530
	UserUnauthorizedRole HttpCode = 100531
	FileUploadFail       HttpCode = 100532
)

var Codes = map[HttpCode]string{
	Success:              "操作成功",
	Failed:               "操作失败",
	LoginFailed:          "登录失败",
	DataNotExist:         "数据不存在",
	UserPasswordError:    "账号密码不正确",
	AuthNotExist:         "认证信息不正确",
	AuthFail:             "校验认证信息失败",
	RequestParamError:    "请求参数错误",
	UserNameNotExist:     "用户名不存在",
	UserNameExist:        "账号已经存在",
	PhoneExist:           "用户手机号已经存在",
	EmailExist:           "用户邮箱已经注册",
	TokenBuildError:      "生成Token错误",
	TokenTimeOut:         "认证信息过期",
	AddDataError:         "增加数据失败",
	SqlExecuteError:      "SQL执行错误",
	DeleteSuccess:        "删除成功",
	ExistSameData:        "存在相同的数据",
	SoftwareCreateError:  "软件创建失败",
	DataNotNeedUpdate:    "数据不需要修改",
	DataDeleteFail:       "数据删除失败",
	LogoutSuccess:        "退出登录成功",
	LogoutFail:           "退出登录失败",
	DateUpdateError:      "数据更新失败",
	CaptchaImageError:    "验证码生成失败",
	UserNotAllowDelete:   "当前用户不允许删除",
	LoginSuccess:         "登录成功",
	RegisterReqParseFail: "注册用户请求实体解析失败",
	RegisterUserFail:     "注册用户失败",
	QueryDataFail:        "查询数据异常",
	UserForbidden:        "用户禁用",
	SystemFail:           "系统异常",
	RefreshTokenFail:     "刷新Token失败",
	UserUnauthorizedRole: "用户未授权角色",
	FileUploadFail:       "文件上传异常",
}

func result[T any](code HttpCode, data T) ResultMessage[T] {
	return ResultMessage[T]{
		Code:    code,
		Message: Codes[code],
		Data:    data,
	}
}

func Message[T any](code HttpCode, data T) ResultMessage[T] {
	return result[T](code, data)
}

func Ok[T any](data T) ResultMessage[T] {
	return result[T](Success, data)
}

func Fail[T any](code HttpCode, msg T) ResultMessage[T] {
	return result[T](code, msg)
}

var (
	UserPasswdError = errors.New(Codes[UserPasswordError])
)
